﻿using System.Text;

namespace Sage.MarkdownRenderer.Html
{
    /// <summary>
    /// 颜色配置类
    /// 用于自定义各种元素的颜色
    /// </summary>
    public class ColorConfig
    {
        #region 基础颜色
        /// <summary>
        /// 主要颜色（链接、按钮等）
        /// </summary>
        public string? PrimaryColor { get; set; }

        /// <summary>
        /// 强调颜色（悬停效果等）
        /// </summary>
        public string? AccentColor { get; set; }

        /// <summary>
        /// 成功颜色（用于成功提示等）
        /// </summary>
        public string? SuccessColor { get; set; }

        /// <summary>
        /// 警告颜色（用于警告提示等）
        /// </summary>
        public string? WarningColor { get; set; }

        /// <summary>
        /// 错误颜色（用于错误提示等）
        /// </summary>
        public string? ErrorColor { get; set; }

        /// <summary>
        /// 信息颜色（用于信息提示等）
        /// </summary>
        public string? InfoColor { get; set; }
        #endregion

        #region 边框和分隔线颜色
        /// <summary>
        /// 边框颜色（表格、代码块等）
        /// </summary>
        public string? BorderColor { get; set; }

        /// <summary>
        /// 引用块左边框颜色
        /// </summary>
        public string? BlockquoteBorderColor { get; set; }

        /// <summary>
        /// 水平分隔线颜色
        /// </summary>
        public string? HorizontalRuleColor { get; set; }
        #endregion

        #region 代码相关颜色
        /// <summary>
        /// 行内代码背景色
        /// </summary>
        public string? InlineCodeBackground { get; set; }

        /// <summary>
        /// 行内代码文字颜色
        /// </summary>
        public string? InlineCodeColor { get; set; }

        /// <summary>
        /// 代码块背景色
        /// </summary>
        public string? CodeBlockBackground { get; set; }
        #endregion

        #region 表格颜色
        /// <summary>
        /// 表格表头背景色
        /// </summary>
        public string? TableHeaderBackground { get; set; }

        /// <summary>
        /// 表格偶数行背景色
        /// </summary>
        public string? TableEvenRowBackground { get; set; }

        /// <summary>
        /// 表格悬停行背景色
        /// </summary>
        public string? TableHoverBackground { get; set; }
        #endregion

        #region 列表颜色
        /// <summary>
        /// 列表标记颜色（圆点、数字等）
        /// </summary>
        public string? ListMarkerColor { get; set; }
        #endregion

        /// <summary>
        /// 生成自定义 CSS 变量
        /// </summary>
        /// <returns>CSS 变量定义字符串</returns>
        public string GenerateCustomCssVariables()
        {
            var css = new StringBuilder();
            css.AppendLine(":root {");

            if (!string.IsNullOrEmpty(PrimaryColor))
                css.AppendLine($"    --primary-color: {PrimaryColor} !important;");

            if (!string.IsNullOrEmpty(AccentColor))
                css.AppendLine($"    --accent-color: {AccentColor} !important;");

            if (!string.IsNullOrEmpty(SuccessColor))
                css.AppendLine($"    --success-color: {SuccessColor} !important;");

            if (!string.IsNullOrEmpty(WarningColor))
                css.AppendLine($"    --warning-color: {WarningColor} !important;");

            if (!string.IsNullOrEmpty(ErrorColor))
                css.AppendLine($"    --error-color: {ErrorColor} !important;");

            if (!string.IsNullOrEmpty(InfoColor))
                css.AppendLine($"    --info-color: {InfoColor} !important;");

            if (!string.IsNullOrEmpty(BorderColor))
                css.AppendLine($"    --border-color: {BorderColor} !important;");

            if (!string.IsNullOrEmpty(BlockquoteBorderColor))
                css.AppendLine($"    --blockquote-border-color: {BlockquoteBorderColor} !important;");

            if (!string.IsNullOrEmpty(HorizontalRuleColor))
                css.AppendLine($"    --hr-color: {HorizontalRuleColor} !important;");

            if (!string.IsNullOrEmpty(InlineCodeBackground))
                css.AppendLine($"    --inline-code-bg: {InlineCodeBackground} !important;");

            if (!string.IsNullOrEmpty(InlineCodeColor))
                css.AppendLine($"    --inline-code-color: {InlineCodeColor} !important;");

            if (!string.IsNullOrEmpty(CodeBlockBackground))
                css.AppendLine($"    --code-block-bg: {CodeBlockBackground} !important;");

            if (!string.IsNullOrEmpty(TableHeaderBackground))
                css.AppendLine($"    --table-header-bg: {TableHeaderBackground} !important;");

            if (!string.IsNullOrEmpty(TableEvenRowBackground))
                css.AppendLine($"    --table-even-row-bg: {TableEvenRowBackground} !important;");

            if (!string.IsNullOrEmpty(TableHoverBackground))
                css.AppendLine($"    --table-hover-bg: {TableHoverBackground} !important;");

            if (!string.IsNullOrEmpty(ListMarkerColor))
                css.AppendLine($"    --list-marker-color: {ListMarkerColor} !important;");

            css.AppendLine("}");
            return css.ToString();
        }

        /// <summary>
        /// 预定义配色方案 - 蓝色主题
        /// </summary>
        public static ColorConfig BlueTheme => new()
        {
            PrimaryColor = "#0066cc",
            AccentColor = "#004499",
            BlockquoteBorderColor = "#0066cc",
            HorizontalRuleColor = "#0066cc",
            InlineCodeColor = "#0066cc",
            ListMarkerColor = "#0066cc"
        };

        /// <summary>
        /// 预定义配色方案 - 绿色主题
        /// </summary>
        public static ColorConfig GreenTheme => new()
        {
            PrimaryColor = "#28a745",
            AccentColor = "#1e7e34",
            BlockquoteBorderColor = "#28a745",
            HorizontalRuleColor = "#28a745",
            InlineCodeColor = "#28a745",
            ListMarkerColor = "#28a745"
        };

        /// <summary>
        /// 预定义配色方案 - 紫色主题
        /// </summary>
        public static ColorConfig PurpleTheme => new()
        {
            PrimaryColor = "#6f42c1",
            AccentColor = "#5a32a3",
            BlockquoteBorderColor = "#6f42c1",
            HorizontalRuleColor = "#6f42c1",
            InlineCodeColor = "#6f42c1",
            ListMarkerColor = "#6f42c1"
        };
    }
}
